<template>
    <CommonPage ref="funderRef" :tableConfig="option" formKey="funderManage"></CommonPage>
    <FunderManageAddorEdit v-if="dialog" v-model="dialog" :type="openType" :info="rowInfo" @callback="funderRef.resetTableData()"/>
</template>
<script setup>
import CommonPage from '@/components/CommonPage/index.vue'
import FunderManageAddorEdit from './components/funderManageAddorEdit.vue'
import { reactive, ref } from 'vue';
import {funderType, funderStatus} from "@/utils/enum.js"
import {chageStatus, deleteFund} from "@/server/manage/product.js"
import {ElMessage, ElMessageBox} from "element-plus"
const dialog = ref(false)
const openType = ref()
const rowInfo = ref()
const funderRef = ref()
const option = reactive({
    operationWidth: 200,
    formItems: [
        {label: '资金方名称', prop: 'fundName', type: 'input', show: true},
        {label: '分类', prop: 'fundType', type: 'select', show: true, options: funderType},
        {label: '状态', prop: 'fundStatus', type: 'select', show: true, options: funderStatus},
    ],
    buttons: [
        {label: "新增", icon: 'Plus', auth: '', handle: (event)=> {handleOpen('add')}},
        {label: "删除", icon: 'Delete', type: 'danger', handle: (event)=> {handleDel(event)}},
    ],
    options: [
        {label: '停用', handle: (event) => {handleStatus(event, 1)}, isShow:(row) => {return row.fundStatus == 2}, type: "warning"},
        {label: '启用', handle: (event) => {handleStatus(event, 2)}, isShow:(row) => {return row.fundStatus == 1}, type: "success"},
        {label: '编辑', handle: (event) => {handleOpen('edit', event)}, isShow:() => {return true}},
        {label: '删除', handle: (event) => {handleDel(event)}, isShow:() => {return true}, type: 'danger'}
    ]
})
// 新增 编辑
const handleOpen = (type, item) => {
    openType.value = type
    item && (rowInfo.value = item)
    dialog.value = true
}
// 修改状态
const handleStatus = (row, status) => {
    chageStatus({id: row.id, fundStatus: status}).then(res => {
        funderRef.value.getTableData()
        ElMessage({ message: "操作成功！", type: 'success'})
    })
}
//删除
const handleDel = (list) => {
    let ids = Array.isArray(list) ? list.map(item => item.id) : [list.id]
    ElMessageBox.confirm('您确定删除吗？','提示',
        {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning',}
    ).then(() => {
        deleteFund({ids: ids}).then(res => {
            ElMessage({ type: 'success', message: '删除成功！'})
            funderRef.value.resetTableData()
        })
    })
    .catch()
}

</script>